package components.plugins.mlp
{
	import MLP.multiLayerObject;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_close;
	
	import UIUtils.uif_MLPEncoder;
	import UIUtils.uif_util;
	
	import classes.clt_events;
	import classes.clt_locale;
	
	import components.clt_arrowB;
	import components.clt_component;
	import components.clt_plButton;
	import components.clt_popupMenu;
	import components.game.clt_gameParams;
	import components.plugins.frames.clt_framesLayer;
	
	import data.clt_data;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Transform;
	import flash.geom.Vector3D;
	import flash.utils.ByteArray;
	
	public class clt_mlpLayer extends clt_component {
		
		private static const SAVESTEREO: String = ' как анаглифическое изображение ';
		private static const SAVESTEREOTWO: String = ' как стерео-пару ';
		private static const TOGAME: String = ' на конкурс ';
		private var _image: clt_mlpImage;
		public function clt_mlpLayer(a_clt: clt_clothingBase) {
			var rect: Rectangle = a_clt.getInner();
			super(a_clt.toolsBarLayer, rect.x, rect.y, rect.width, rect.height);
		}
		
		public function execute(a_params: Object): void {
			var eventObject: Object = {
				result		: null,
				depthMax	: 100,
				size		: new Point(600, 600)
			}
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.GETRESULTBITMAPS, eventObject));
			if (eventObject.result) doAfterResult(eventObject.result);
		}
		
		protected function doAfterResult(bitmaps: Array): void {
			fx('alpha', 0, 1).start();
			
			var rect	: Rectangle = getInner();
			var back	: clt_component = new clt_component(This, rect.x, rect.y, rect.width, rect.height, 'modalGurtain');
			_image 	= new clt_mlpImage(This);
			_image.assign(bitmaps, true);
			
			rect = clt.resultLayer.display.getRect(clt.topLayer);
			_image.position = rect.topLeft;
			_image.resize(600, 600);
			
			rect = new Rectangle(_image.x + _image.width, _image.y, 140, 33);
			addButton('Закрыть', function(e: Event): void {
				fx('alpha', alpha, 0).start(function(): void {
					disposeChilds();
				})
			}, rect);
			var button: clt_plButton = addButtonSubmenu('Отправить', rect, [
				{
					caption	: clt_locale.SAVETODISK,
					method	: doSaveToFile
				},{
					caption	: TOGAME,
					method	: doGame
				},{
					caption	: clt_locale.SAVETOMYALBUM,
					method	: doAlbum
				},{
					caption	: SAVESTEREO,
					method	: doStereo
				},{
					caption	: SAVESTEREOTWO,
					method	: doStereoTwo
				}
			], function(e: ObjectEvent): void {
				e.object.method();
			}, function(e: Event): void {
			});
		}
		
		private function doStereo(): void {
			stereoSave();
		}
		
		private function doStereoTwo(): void {
			stereoSave();
		}
		
		private function stereoSave(): void {
			
			var bmp: Bitmap = new Bitmap(new BitmapData(_image.width, _image.height, false, 0));
			var c: Point = _image.image.center;
			
			var ptmp	: PerspectiveProjection = clt.transform.perspectiveProjection;
			var pp		: PerspectiveProjection = new PerspectiveProjection();
			pp.projectionCenter = c;
			pp.fieldOfView = 30;
			clt.transform.perspectiveProjection = pp;
/*			
			bmp.x = _image.x;
			bmp.y = _image.y;
			addChild(bmp);
*/			
			
			function paintOne(angle: Number, ax: Number, cm: ColorTransform=null): void {
				var mat: Matrix3D = new Matrix3D();
				mat.appendTranslation(-c.x, -c.y, 0);
				mat.appendRotation(angle, Vector3D.Y_AXIS);
				mat.appendTranslation(c.x, c.y, 0);
				
				_image.image.layer3D.transform.matrix3D = mat;
				
				var a_tmp: BitmapData = new BitmapData(bmp.bitmapData.width, bmp.bitmapData.height, false);
				var offset: Number = 0.33;
				a_tmp.draw(_image);
				a_tmp.applyFilter(a_tmp, a_tmp.rect, uif_util.NULLPOINT, new ColorMatrixFilter([
					0.6, 0.2, 0.2, 0, 0,
					0.2, 0.6, 0.2, 0, 0,
					0.2, 0.2, 0.6, 0, 0,
					0,0,0,1,0]
				));
				bmp.bitmapData.draw(a_tmp, new Matrix(1, 0, 0, 1, ax), cm, BlendMode.SCREEN);
				
				a_tmp.dispose();
			}
			
			paintOne(-5, 0, new ColorTransform(1, 0, 0, 1));
			paintOne(5, 0, new ColorTransform(0, 1, 1, 1));
			
			clt.transform.perspectiveProjection = ptmp;
			
			clt.saveToFile(clt.encodeFromJPG(bmp.bitmapData), clt.genFileNameCard(clt_data.JPGEXT));
			
			bmp.bitmapData.dispose();
		}
		
		protected function doSaveToFile(): void {
			clt.saveToFile(_image.bytes, clt.genFileNameCard(clt_data.MLPEXT));
		}
		
		private function doAlbum(): void {
			doGame(true);
		}
		
		protected function doGame(isMyAlbum: Boolean=false): void {
			clt.toGameMLP(isMyAlbum, _image);
/*			
			var gp: clt_gameParams = new clt_gameParams(clt.dialogLayer, function(): void {
				clt.getResult(60, 60, function(bmd: BitmapData): void {
					var preview: ByteArray = clt.encodeFromJPG(bmd);
					var bytes: ByteArray = new ByteArray();
					bytes.writeInt(preview.length);
					bytes.writeBytes(preview);
					bytes.writeBytes(_image.bytes);
					bmd.dispose();
					
					clt.internalServer.imageFromGame(bytes, 
						gp.nameCollage, gp.stiker, function(a_data: Object): void {
						if (!a_data.errors && a_data.response && !isMyAlbum) 
							workspace.dispatchEvent(new Event(clt_events.UPDATEGAME));
						
						bytes.clear();
					}, true, isMyAlbum);
				});
			}); 
			gp.show();
*/			
		}
	}
}